import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-local';

/**
 * 本地认证策略
 * 基于 NestJS 官方文档实现
 * 参考: https://docs.nestjs.cn/security/authentication
 */
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
  constructor() {
    super({
      usernameField: 'username',
      passwordField: 'password',
    });
  }

  /**
   * 验证用户凭据
   */
  async validate(username: string, password: string): Promise<any> {
    // 这里应该调用用户服务来验证凭据
    // 暂时返回模拟数据，实际实现时需要注入用户服务
    if (username === 'admin' && password === 'admin') {
      return {
        userId: 1,
        username: 'admin',
        roles: ['admin'],
        siteId: 1,
      };
    }

    throw new UnauthorizedException('Invalid credentials');
  }
}
